<template>
    <div class="app-container">
      <!-- 简化的搜索表单 -->
      <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
        <el-form-item label="资讯标题" prop="title">
          <el-input
            v-model="queryParams.title"
            placeholder="输入关键词搜索"
            clearable
            @keyup.enter="handleQuery"
            class="search-input"
          />
        </el-form-item>
        <el-form-item label="类型" prop="infoType">
          <el-select
            v-model="queryParams.infoType"
            placeholder="全部类型"
            clearable
            style="width: 120px;"
          >
            <el-option
              v-for="dict in info_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 资讯列表展示 -->
      <el-table
        v-loading="loading"
        :data="infoList"
        style="width: 100%"
        :default-sort="{ prop: 'publishTime', order: 'descending' }"
      >
        <!-- 隐藏ID列 -->
        <el-table-column label="资讯标题" prop="title">
          <template #default="scope">
            <div class="info-title">
              <el-image
                :src="scope.row.coverImg"
                :preview-src-list="[scope.row.coverImg]"
                fit="cover"
                class="thumb-img"
              />
              <span class="title-text">{{ scope.row.title }}</span>
            </div>
          </template>
        </el-table-column>

        <el-table-column label="类型" prop="infoType" width="120">
          <template #default="scope">
            <dict-tag
              :options="info_type"
              :value="scope.row.infoType"
              type="success"
            />
          </template>
        </el-table-column>

        <el-table-column label="发布时间" prop="publishTime" width="180" sortable>
          <template #default="scope">
            <div class="publish-time">
              <i class="el-icon-time"></i>
              {{ parseTime(scope.row.publishTime, '{y}-{m}-{d}') }}
            </div>
          </template>
        </el-table-column>

        <el-table-column label="状态" prop="status" width="100">
          <template #default="scope">
            <status-badge :status="scope.row.status">
              <dict-tag :options="publish_status" :value="scope.row.status"/>
            </status-badge>
          </template>
        </el-table-column>

        <el-table-column label="操作" width="80">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="handleViewDetail(scope.row)"
            >详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 资讯详情对话框 -->
      <el-dialog
        :title="currentInfo.title"
        v-model="detailVisible"
        width="800px"
        top="5vh"
      >
        <div class="info-detail">
          <div class="cover-img">
            <el-image :src="currentInfo.coverImg" fit="cover"/>
          </div>
          <div class="meta-info">
            <div class="meta-item">
              <label>发布时间：</label>
              <span>{{ parseTime(currentInfo.publishTime) }}</span>
            </div>
            <div class="meta-item">
              <label>资讯类型：</label>
              <dict-tag :options="info_type" :value="currentInfo.infoType"/>
            </div>
            <div class="meta-item">
              <label>当前状态：</label>
              <dict-tag :options="publish_status" :value="currentInfo.status"/>
            </div>
          </div>
          <div class="content-box" v-html="currentInfo.content"></div>
        </div>
      </el-dialog>
    </div>
  </template>

  <script setup>
  import { ref, reactive } from 'vue'
  import { listInfo } from "@/api/healthSys/info"
  import { parseTime } from "@/utils/ruoyi"

  const { proxy } = getCurrentInstance()
  const { info_type, publish_status } = proxy.useDict('info_type', 'publish_status')

  // 数据部分
  const infoList = ref([])
  const loading = ref(true)
  const total = ref(0)
  const queryParams = reactive({
    pageNum: 1,
    pageSize: 10,
    title: null,
    infoType: null,
    status: '1' // 默认只显示已发布
  })

  // 详情相关
  const currentInfo = ref({})
  const detailVisible = ref(false)

  /** 获取数据列表 */
  const getList = async () => {
    try {
      loading.value = true
      const res = await listInfo(queryParams)
      infoList.value = res.rows.map(item => ({
        ...item,
        content: item.content.replace(/<img/g, '<img style="max-width:100%"') // 处理图片响应
      }))
      total.value = res.total
    } catch (error) {
      console.error('获取数据失败:', error)
    } finally {
      loading.value = false
    }
  }

  /** 查看详情 */
  const handleViewDetail = (row) => {
    currentInfo.value = row
    detailVisible.value = true
  }

  // 初始加载
  getList()
  </script>

  <style lang="scss" scoped>
  .info-title {
    display: flex;
    align-items: center;
    .thumb-img {
      width: 60px;
      height: 40px;
      margin-right: 12px;
      border-radius: 4px;
    }
    .title-text {
      flex: 1;
      @include text-ellipsis;
    }
  }

  .publish-time {
    color: $--color-text-secondary;
    i {
      margin-right: 4px;
    }
  }

  .info-detail {
    .cover-img {
      margin-bottom: 20px;
      :deep(.el-image) {
        width: 100%;
        height: 300px;
        border-radius: 8px;
      }
    }

    .meta-info {
      display: grid;
      grid-template-columns: repeat(2, 1fr);
      gap: 12px;
      margin-bottom: 20px;
      .meta-item {
        display: flex;
        align-items: center;
        label {
          color: $--color-text-secondary;
          min-width: 80px;
        }
      }
    }

    .content-box {
      line-height: 1.8;
      :deep(img) {
        max-width: 100%;
        margin: 10px 0;
      }
    }
  }

  .search-input {
    width: 240px;
  }
  </style>
